#include "union_config.h"
#include "time_module.h"
#include "string_common.h"
#include "log.h"

const CUnionBossLv* CUnionBossCfg::Getbossinfo(int32_t level)const
{
	auto it = unionboss_config_.find(level);
	if ( it == unionboss_config_.end() )
	{
		return NULL;
	}
	return &(it->second);
}

void CUnionBossCfg::LoadFromJson(const Json::Value& unioninfo)
{
	for (int32_t i = 1; i <= unioninfo.size(); ++i )
	{
		std::string index = ::common::ToString(i);
		CUnionBossLv bosslv;
		bosslv.LoadFromJson(unioninfo[index]);

		unionboss_config_.insert(std::make_pair(bosslv.level(), bosslv));
	}
}

void CUnionBossLv::LoadFromJson(const Json::Value& bossinfo)
{
	level_ = bossinfo["level"].asInt();
	monsterid_ = bossinfo["monsterID"].asInt();
	award_firstblood_.LoadFromJson(bossinfo["award_firstblood"]);
	award_kill_.LoadFromJson(bossinfo["award_kill"]);
	award_partake_.LoadFromJson(bossinfo["award_partake"]);
	award_num1_.LoadFromJson(bossinfo["award_num1"]);
}

void CItemTpl::LoadFromJson(const Json::Value& itemjson)
{
	if ( itemjson.size() < 2 )
	{
		return;
	}
	int32_t k = 0;
	itemid = itemjson[k++].asInt();
	count = itemjson[k++].asInt();
}

void CItemTplList::LoadFromJson(const Json::Value& itemlist)
{
	item_json_list_ = itemlist;
	for ( int32_t i = 0; i < itemlist.size(); ++i )
	{
		CItemTpl itemtpl;
		itemtpl.LoadFromJson(itemlist[i]);

		item_list_.push_back(itemtpl);
	}
}

void CItemExpense::LoadFromJson(const Json::Value& itemjson)
{
	if (itemjson.size() < 4)
	{
		return;
	}
	int32_t k = 0;
	begin_id = itemjson[k++].asInt();
	end_id = itemjson[k++].asInt();
	itemid = itemjson[k++].asInt();
	count = itemjson[k++].asInt();
}


void CItemTplList::GetMailJsonItem(Json::Value& maillist) const
{
	for (TITEM_LIST::const_iterator it = item_list_.begin(); it != item_list_.end(); ++it )
	{
		Json::Value oneitem;
		oneitem["id"] = (int32_t)it->itemid;
		oneitem["at"] = (int32_t)it->count;
		maillist.append(oneitem);
	}
}
